package com.shensi.adminCollect.controller.label;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.label.LabelRuleDetail;
import com.shensi.adminCollect.service.label.ILabelRuleDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;

/**
 * 标签规则明细Controller
 * 
 * @author qujia
 * @date 2021-06-15
 */
@RestController
@RequestMapping("/label/detail")
@Api("标签规则明细Controller")
public class LabelRuleDetailController extends BaseController
{
    @Autowired
    private ILabelRuleDetailService labelRuleDetailService;

    /**
     * 查询标签规则明细列表
     */
    @GetMapping("/list")
    @ApiOperation("查询标签规则明细列表")
    public TableDataInfo list(LabelRuleDetail labelRuleDetail)
    {
        startPage();
        List<LabelRuleDetail> list = labelRuleDetailService.selectLabelRuleDetailList(labelRuleDetail);
        return getDataTable(list);
    }

    @GetMapping("/detailList")
    @ApiOperation("查询标签明细列表")
    public TableDataInfo detailList(LabelRuleDetail labelRuleDetail)
    {
        labelRuleDetail.setDelFlag("0");
        List<LabelRuleDetail> list = labelRuleDetailService.selectLabelRuleDetailList(labelRuleDetail);
        return getDataTable(list);
    }

    /**
     * 导出标签规则明细列表
     */
    @GetMapping("/export")
    @ApiOperation("导出标签规则明细列表")
    public AjaxResult export(LabelRuleDetail labelRuleDetail)
    {
        List<LabelRuleDetail> list = labelRuleDetailService.selectLabelRuleDetailList(labelRuleDetail);
        ExcelUtil<LabelRuleDetail> util = new ExcelUtil<LabelRuleDetail>(LabelRuleDetail.class);
        return util.exportExcel(list, "detail");
    }

    /**
     * 获取标签规则明细详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取标签规则明细详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(labelRuleDetailService.selectLabelRuleDetailById(id));
    }

    /**
     * 新增标签规则明细
     */
    @PostMapping
    @ApiOperation("新增标签规则明细")
    public AjaxResult add(@RequestBody LabelRuleDetail labelRuleDetail)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        labelRuleDetail.setCreateBy(currentUser.getUser().getUserId()+"");
        labelRuleDetail.setDelFlag("0");
        return toAjax(labelRuleDetailService.insertLabelRuleDetail(labelRuleDetail));
    }

    /**
     * 修改标签规则明细
     */
    @PutMapping
    @ApiOperation("修改标签规则明细")
    public AjaxResult edit(@RequestBody LabelRuleDetail labelRuleDetail)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        labelRuleDetail.setUpdateBy(currentUser.getUser().getUserId()+"");
        labelRuleDetail.setDelFlag("1");
        return toAjax(labelRuleDetailService.updateLabelRuleDetail(labelRuleDetail));
    }

    @PostMapping(value ="/deleteLabelRuleDetail")
    @Transactional
    @ApiOperation("积分商品管理")
    public AjaxResult deleteLabelRuleDetail(@RequestBody LabelRuleDetail labelRuleDetail)
    {
        int i=0;
        try {
            LoginUser currentUser = SecurityUtils.getLoginUser();
            labelRuleDetail.setDelFlag("1");
            labelRuleDetail.setUpdateBy(currentUser.getUser().getUserId()+"");
           i= labelRuleDetailService.updateLabelRuleDetail(labelRuleDetail);
        }catch (Exception e){
            System.out.println("------------------------------------"+e.getLocalizedMessage()+"");
            e.printStackTrace();
        }
        return toAjax(i);

    }
    /**
     * 删除标签规则明细
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除标签规则明细")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(labelRuleDetailService.deleteLabelRuleDetailByIds(ids));
    }

}
